#include "PauseMenuState.h"
#include "SinglePlayerState.h"
#include "OptionsMenuState.h"
#include "MainMenuState.h"
#include "SaveMenuState.h"
#include "LoadMenuState.h"
#include "CSGD_DirectInput.h"
#include "CSGD_TextureManager.h"
#include "CSGD_XAudio2.h"
#include "LevelManager.h"
#include "EntityManager.h"
#include "Game.h"
#include "SaveSystem.h"
#include "BitmapFont.h"
#include "../resource.h"
using namespace std;

CPauseMenuState::CPauseMenuState(void)
{
	m_nCursor = 0;
	m_nBackgroundImg = -1;
	m_pPrevState = nullptr;
}


CPauseMenuState::~CPauseMenuState(void)
{
}

/////////////////////////////////////////////////////////////////////////////
// Function		: GetInstance 
// Return		: COptionsMenuState* 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
CPauseMenuState* CPauseMenuState::GetInstance( void )
{
	static CPauseMenuState s_Instance;

	return &s_Instance;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Enter 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CPauseMenuState::Enter( void )
{
	m_nCursor = 0;
	m_nBackgroundImg = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/MenuBackground.png"));

	if(COptionsMenuState::GetInstance()->ReturnEng())
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_Save, Save, 10);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_Load, Load, 10);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_Options, Options, 30);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_MainMenu, MainMenu, 50);
	}
	else
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_Save, Save, 10);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_Load, Load, 10);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_Options, Options, 30);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_MainMenu, MainMenu, 50);
	}

}
/////////////////////////////////////////////////////////////////////////////
// Function		: Exit 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CPauseMenuState::Exit ( void )
{
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBackgroundImg);
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Input 
// Return		: bool
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
bool CPauseMenuState::Input( void )
{
	CSGD_DirectInput* m_pDI = CSGD_DirectInput::GetInstance();

	if((m_pDI->KeyPressed(DIK_UP)  || m_pDI->JoystickDPadPressed( DIR_UP ) == true) && m_nCursor > 0)
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetMenuMovID());
		m_nCursor--;
	}

	else if((m_pDI->KeyPressed(DIK_DOWN) || m_pDI->JoystickDPadPressed( DIR_DOWN ) == true) && m_nCursor < 3)
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetMenuMovID());
		m_nCursor++;
	}
	if (m_pDI->KeyPressed(DIK_RETURN) || m_pDI->JoystickButtonPressed(0,0) == true )
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetConfirmID());
		if(m_nCursor == 0)
		{
			CGame::GetInstance()->ChangeState(CSaveMenuState::GetInstance());
		}
		else if (m_nCursor == 1)
		{
			CGame::GetInstance()->ChangeState(CLoadMenuState::GetInstance());
		}

		else if(m_nCursor == 2)
		{
			COptionsMenuState::GetInstance()->SetPrevState(*this);
			CGame::GetInstance()->ChangeState(COptionsMenuState::GetInstance());
		}

		else if (m_nCursor == 3)
		{
			CLevelManager::GetInstance()->GetCurrLevel()->Exit();
			CEntityManager::GetInstance()->Clear();

			if( GetPrevState() == CSinglePlayerState::GetInstance() )
				CSinglePlayerState::GetInstance()->bPause = false;
			CGame::GetInstance()->ChangeState(CMainMenuState::GetInstance());
		}

	}

	if( m_pDI->KeyPressed( DIK_ESCAPE ) == true || m_pDI->JoystickButtonPressed(1,0) )
		CGame::GetInstance()->ChangeState(m_pPrevState);		

	return true;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Update 
// Parameters	: fElapsedTime - Ammount of time passed since last update
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CPauseMenuState::Update( float fElapsedTime )
{
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Render
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CPauseMenuState::Render( void )
{
	CSGD_TextureManager::GetInstance()->Draw(m_nBackgroundImg, 0,0, 1.0f,1.0f);

	CBitmapFont* font = CGame::GetInstance()->GetFont();

	font->Draw( _T(">"), 50, 100 + (100 * m_nCursor), 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

	font->Draw( Save, 100, 100 , 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

	font->Draw( Load, 100, 200, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

	font->Draw( Options, 100, 300, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

	font->Draw(MainMenu, 100, 400, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
}